Component({
  data: {
    selected: 0,
    color: '#999999',
    selectedColor: '#ff6b9d',
    list: [
      {
        pagePath: '/pages/index/index',
        text: '首页',
        iconName: 'home'
      },
      {
        pagePath: '/pages/category/category',
        text: '分类',
        iconName: 'category'
      },
      {
        pagePath: '/pages/cart/cart',
        text: '购物车',
        iconName: 'cart'
      },
      {
        pagePath: '/pages/profile/profile',
        text: '我的',
        iconName: 'profile'
      }
    ]
  },

  attached() {
    this.updateSelected();
  },

  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset;
      const url = data.path;
      wx.switchTab({ url });
      this.setData({
        selected: data.index
      });
    },

    updateSelected() {
      const pages = getCurrentPages();
      if (pages.length === 0) return;
      
      const currentPage = pages[pages.length - 1];
      if (!currentPage || !currentPage.route) return;
      
      const currentRoute = '/' + currentPage.route;
      
      const selected = this.data.list.findIndex(item => item.pagePath === currentRoute);
      if (selected !== -1) {
        this.setData({
          selected: selected
        });
      }
    }
  }
});